const { ccclass, property } = cc._decorator;

@ccclass
class FindPathMgr {
    pathArr;
    pathCount;
    finalStrArr;

    constructor() {
        this.pathArr = [];
        this.pathCount = 0;
        this.finalStrArr = [];
    }

    static _instance:FindPathMgr;
    static _getInstance():FindPathMgr {
        if (!FindPathMgr._instance) {
            FindPathMgr._instance = new FindPathMgr();
        }
        return FindPathMgr._instance;
    }

    creatNewPath() {
        this.pathCount += 1;
        this.pathArr.push([]);
        console.log("creatNewPath=======creatNewPath", this.pathArr);
    }

    getPathArr() {
        return this.pathArr;
    }

    addPosToPath(pos) {
        const lastPath = this.pathArr[this.pathArr.length - 1];
        if (lastPath) {
            const index = this.checkSamePos(lastPath, pos);
            if (index === -1) {
                const newPos = Object.assign({}, pos, {
                    isHavePalce: true
                });
                lastPath.push(newPos);
            } else {
                lastPath[index].isHavePalce = true;
            }
        }
        console.log("当前的路径数据=======", this.pathArr);
    }

    checkSamePos(path, pos) {
        for (let i = 0; i < path.length; i++) {
            const p = path[i];
            if (p.row === pos.row && p.col === pos.col) {
                return i;
            }
        }
        return -1;
    }

    deletePos(pos) {
        const lastPath = this.pathArr[this.pathArr.length - 1];
        if (lastPath) {
            const index = this.checkSamePos(lastPath, pos);
            if (index !== -1) {
                lastPath.splice(index, 1);
            }
        }
    }

    fitleEmptyArr() {
        this.pathArr = this.pathArr.filter(path => path.length > 0);
    }

    setFinalStr() {
        const strArr = [];
        this.fitleEmptyArr();
        console.log("最终的路径", this.pathArr);
        for (let i = 0; i < this.pathArr.length; i++) {
            const path = this.pathArr[i];
            let str = "O-27_" + path.length + "_";
            for (let j = 0; j < path.length; j++) {
                const pos = path[j];
                str += j === path.length - 1 ? `${pos.row}~${pos.col}` : `${pos.row}~${pos.col}&`;
            }
            strArr.push(str);
        }
        this.finalStrArr = strArr;
        console.log("最终的strArr", strArr);
        return strArr;
    }

    getInitStrArr() {
        return this.finalStrArr;
    }

    clearPath() {
        this.pathArr = [];
        this.pathCount = 0;
        this.finalStrArr = [];
    }

    getInitPosData() {
        const posData = [];
        for (let i = 0; i < this.finalStrArr.length; i++) {
            const str = this.finalStrArr[i];
            const initStr = this.converInitStr(str);
            posData.push(initStr);
        }
        return posData;
    }

    getPosDataByStr(str) {
        return this.converInitStr(str);
    }

    converInitStr(str) {
        return str
            .split("_")[2]
            .split("&")
            .map(item => {
                const [row, col] = item.split("~").map(Number);
                return {
                    row,
                    col
                };
            });
    }

    getTotalCountByStr(str) {
        const startIndex = str.indexOf("_") + 1;
        const endIndex = str.indexOf("_", startIndex);
        const count = endIndex > startIndex ? str.substring(startIndex, endIndex) : null;
        return parseInt(count);
    }
}

const instance = FindPathMgr._getInstance();
export default instance; 